import { pay } from '@/api/modules/pay'
import store from '@/store'
import monent from 'moment'

// this.$router.replace({ path: '/regist', query: this.$route.query })
export const saveShareInfo = str => {
  return new Promise((resolve, reject) => {
    try {
      let search = null
      if (str) {
        search = str.split('?')[1] ? str.split('?')[1].split('&')[0] : null
      } else {
        search = window.location.href.split('?')[1]
          ? window.location.href.split('?')[1].split('&')[0]
          : null
      }
      if (search) {
        const params = JSON.parse(atob(decodeURIComponent(search)))
        pay.saveShareInfoApi({
          url: window.location.origin + window.location.search,
          shareFrom: params.shareType,
          shareUser: params.shareUser,
          shareTime: params.shareTime,
          createTime: monent(),
          replyUser: store.state.user.id,
          replyType: sessionStorage.getItem('isRegister') ? '2' : '1'
        })
        sessionStorage.removeItem('isRegister')
      }

      resolve()
    } catch (error) {
      reject(error)
    }
  })
}
